package com.ehome.poct.terminal.controller;

import com.ehome.poct.base.api.dto.device.*;
import com.ehome.poct.base.api.feign.ClinicClient;
import com.ehome.poct.base.api.feign.DeviceClient;
import com.ehome.poct.base.api.feign.PayOrderFrontLimitTermClinicFeginClient;
import com.ehome.poct.base.api.feign.device.TestDeviceNewZVersionUpgradeFeginClient;
import com.ehome.poct.base.api.feign.sys.SysDictDataClient;
import com.ehome.poct.base.api.qry.sys.SysDictDataQry;
import com.ehome.poct.base.api.vo.device.*;
import com.ehome.poct.base.api.vo.sys.ProtocolVO;
import com.ehome.poct.terminal.api.dto.QrCodeHisDTO;
import com.ehome.poct.terminal.api.vo.AgentLimitInfoVO;
import com.ehome.poct.terminal.api.vo.DeviceExtInfoVO;
import com.ehome.poct.terminal.service.DeviceService;
import com.ehome.web.annotation.RequestLog;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 设备
 *
 * @author 瑶苏
 * @since 2022/03/03 11:20
 **/
@Api(tags = "设备管理")
@ApiSupport(author = "瑶苏")
@RequestMapping("/device")
@RestController
@AllArgsConstructor
public class DeviceController {
    private final DeviceClient deviceClient;
    private final SysDictDataClient dictDataClient;
    private final ClinicClient clinicClient;
    private final DeviceService deviceService;
    private final PayOrderFrontLimitTermClinicFeginClient orderLinitClient;
    private final TestDeviceNewZVersionUpgradeFeginClient upgradeFeginClient;

    @ApiOperation("判断设备号是否存在")
    @ApiOperationSupport(author = "瑶苏")
    @GetMapping("/isExistence/{sno}")
    @RequestLog
    public Boolean isExistence(@PathVariable @ApiParam(value = "设备sno", required = true) String sno) {
        return deviceClient.isExistence(sno);
    }

    @ApiOperation("根据sno获取设备诊所相关信息")
    @ApiOperationSupport(author = "瑶苏")
    @GetMapping("/getDeviceBySno")
    @RequestLog
    public List<TestDeviceClinicVO> getDeviceBySno(TestDeviceClinicDTO testDeviceClinicDTO) {
        return deviceClient.getDeviceBySno(testDeviceClinicDTO);
    }


    @ApiOperation("新增设备")
    @ApiOperationSupport(author = "瑶苏")
    @PostMapping("/addDevice")
    @RequestLog
    public Boolean addDevice(@RequestBody AddDeviceDTO addDeviceDTO) {
        return deviceClient.addDevice(addDeviceDTO);
    }

    @ApiOperation("设备完成组装")
    @ApiOperationSupport(author = "瑶苏")
    @PutMapping("/finishAssemble/{sno}")
    @RequestLog
    public void finishAssemble(@PathVariable String sno) {
        deviceClient.updateStatus(new DeviceStatusDTO(sno, 3));
    }

    @ApiOperation("工厂模式")
    @ApiOperationSupport(author = "瑶苏")
    @PostMapping("/addFactoryMode")
    @RequestLog
    public boolean addFactoryMode(@RequestBody AddFactoryModeDTO addFactoryModeDTO) {
        return deviceClient.addFactoryModee(addFactoryModeDTO);
    }

    @ApiOperation("新增和修改设备配置文件")
    @ApiOperationSupport(author = "瑶苏")
    @PostMapping("/setDeviceParam")
    @RequestLog
    public boolean setDeviceParam(@RequestBody TestDeviceParamDTO testDeviceParamDTO) {
        return deviceClient.setDeviceParam(testDeviceParamDTO);
    }

    @ApiOperation("获取设备的配置信息")
    @ApiOperationSupport(author = "瑶苏")
    @GetMapping("/getDeviceParam")
    @RequestLog
    public TestDeviceParamVO getDeviceParamNew(TestDeviceParamDTO testDeviceParamDTO) {
        return deviceClient.getDeviceParamNew(testDeviceParamDTO);
    }

    @ApiOperation("获取用户协议和隐私政策")
    @ApiOperationSupport(author = "wyc")
    @GetMapping("/protocol/{type}")
    public ProtocolVO getProtocol(@PathVariable @ApiParam("设备类型 1：金标仪；2：血常规") Integer type) {
        if (type == 1) {
            final var agreement = dictDataClient.getOne(new SysDictDataQry().setDictLabel("USER_AGREEMENT_URL"));
            final var privacy = dictDataClient.getOne(new SysDictDataQry().setDictLabel("USER_PRIVACYSTATEMENT_URL"));
            return new ProtocolVO(agreement.getDictValue(), privacy.getDictValue());
        }
        final var agreement = dictDataClient.getOne(new SysDictDataQry().setDictLabel("USER_AGREEMENT_URL_BLD"));
        final var privacy = dictDataClient.getOne(new SysDictDataQry().setDictLabel("USER_PRIVACYSTATEMENT_URL_BLD"));
        return new ProtocolVO(agreement.getDictValue(), privacy.getDictValue());
    }

    @GetMapping("/{id}")
    TestDeviceVO getById(@PathVariable Long id) {
        return deviceClient.getById(id);
    }

    @ApiOperation("获取设备的额外业务信息")
    @ApiOperationSupport(author = "cq", order = 1)
    @GetMapping("/deviceExtInfo")
    public DeviceExtInfoVO deviceExtInfo(@RequestParam String sno, @RequestParam String projectName) {
        return deviceService.getExtInfo(sno);
    }

    @ApiOperation("获取省总是否具有受限地区报告权限")
    @ApiOperationSupport(author = "cq", order = 1)
    @GetMapping("/hasLimitRight/{provinceId}")
    public AgentLimitInfoVO hasLimitRight(@PathVariable Long provinceId) {
        AgentLimitInfoVO vo = new AgentLimitInfoVO();
        Boolean hasrigit = deviceService.hasLimitRight(provinceId);
        vo.setHasRight(hasrigit);
        return vo;
    }

    @ApiOperation("更新设备连接打印机信息")
    @ApiOperationSupport(author = "muqiu", order = 1)
    @PostMapping("/devicePrinter")
    public void devicePrinter(@RequestBody TestDevicePrinterDTO testDevicePrinterDTO) {
        deviceService.devicePrinter(testDevicePrinterDTO);
    }

    @ApiOperation("获取设备连接打印机信息")
    @ApiOperationSupport(author = "muqiu", order = 1)
    @GetMapping("/getDevicePrinter/{sno}")
    public List<TestDevicePrinterVO> getDevicePrinter(@PathVariable String sno) {
        return deviceService.getDevicePrinter(sno);
    }

    @ApiOperation("添加扫码打印记录")
    @ApiOperationSupport(author = "cq", order = 12)
    @PostMapping("qrcodeprintLog")
    public Boolean qrCodePrintLog(@RequestBody QrCodeHisDTO qrCodeHisDTO) {
        return deviceService.addPrintHistory(qrCodeHisDTO);
    }

    @ApiOperation("获取扫码打印记录列表")
    @ApiOperationSupport(author = "cq", order = 13)
    @GetMapping("/hlist/{pageIndex}/{pageSize}")
    public QrcodePrintHisPageVO hlist(@PathVariable Integer pageIndex, @PathVariable Integer pageSize) {
        return deviceService.hlist(pageIndex, pageSize);
    }

    @ApiOperation("获取扫码打印记录列表详细数据")
    @ApiOperationSupport(author = "cq", order = 14)
    @PostMapping("/dlist")
    public QrcodePrintHisDetPageVO dlist(@RequestBody QrCodeHisDetailDTO req) {
        return deviceService.dlist(req);
    }

    @ApiOperation("获取设备是否在线")
    @ApiOperationSupport(author = "cq", order = 15)
    @GetMapping("/deviceStatus/{sno}")
    public Boolean getDeviceStatus(@PathVariable String sno) {
        return deviceService.getDeviceStatus(sno);
    }

    /**
     * 添加开机自检详细
     *
     * @param bootingInfoDTO 开机自检信息
     * @return true 已录入 false 失败
     */
    @ApiOperation("血常规设备开机自检数据")
    @ApiOperationSupport(author = "cq", order = 16)
    @PostMapping("pushBootingCheckInfo")
    @RequestLog
    public Boolean pushBootingCheckInfo(@RequestBody DeviceBootingInfoDTO bootingInfoDTO) {
        return deviceService.addBootInfoHis(bootingInfoDTO);
    }

    /**
     * 校验设诊所是否具有支付前置功能权限
     *
     * @param id 诊所ID
     * @return TRUE具有支付前置权限 false 不具有
     */
    @ApiOperation("校验设诊所是否具有支付前置功能权限")
    @ApiOperationSupport(author = "laobai", order = 17)
    @GetMapping("/checkorderright/{id}")
    public Boolean checkCanUserOrderFront(@PathVariable(value = "id") @ApiParam("id 为诊所ID termClinicId") Long id) {
        return orderLinitClient.getById(id) != null;
    }

    @ApiOperation("新Z轴升级到4.0体系")
    @ApiOperationSupport(author = "laobai", order = 18)
    @GetMapping("/newzaxisupgrage/{sno}")
    public Boolean newZaxisUpgrade(@PathVariable(value = "sno") @ApiParam("血常规设备sno号") String sno) {
        if (null != upgradeFeginClient.queryOne(new DeviceSnoQry().setSno(sno))) return true;
        return null != upgradeFeginClient.addNewSZxisUgradeRec(new TestDeviceNewZVersionUpgradeDTO().setSno(sno));
    }

}
